package com.jy.adscm.hcPlan.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hcPlan.bean.HcPlanMakePartMaterielExample;
import com.jy.adscm.hcPlan.service.HcPlanMakePartMaterielExampleService;
import com.jy.adscm.hcPlan.service.HcPlanMaterielPriceService;
import com.jy.adscm.support.SimpleCoreResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 * 单批次零件制造对应的物料需求 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2018-08-23
 */
@RestController
@RequestMapping("/adscm/hcplan/makePartMateriel")
public class HcPlanMakePartMaterielExampleController {


    @Autowired
    HcPlanMakePartMaterielExampleService hcPlanMakePartMaterielExampleService;
    @Autowired
    HcPlanMaterielPriceService hcPlanMaterielPriceService;


    /**
     * 获取所有零件对应的物料需求
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllPartMateriel")
    public SimpleCoreResponse getAllPartMateriel() throws AdscmSystemException {

        return new SimpleCoreResponse("200","查询成功",hcPlanMakePartMaterielExampleService.getAllMakePartMaterielExample());
    }
    /**
     * 装配单批次零件制造的对应的物料需求数据
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/loadPartMateriel/{assembly_id}")
    public SimpleCoreResponse loadPartMateriel(@PathVariable Long assembly_id) throws AdscmSystemException {

        hcPlanMakePartMaterielExampleService.loadHcPartMaterielExampleByAssemblyId(assembly_id);
        return new SimpleCoreResponse("200","装配成功",null);
    }
    /**
     * 获取所有物料的供应商
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllSupplier")
    public SimpleCoreResponse getAllSupplier() throws AdscmSystemException {

        return new SimpleCoreResponse("200","获取成功",hcPlanMakePartMaterielExampleService.getAllSupplier());
    }

    /**
     * 物料采购并单
     * @param materiel_purchase_time 开始采购时间
     * @param materiel_arrived_time 采购到货时间
     * @param supplier_id 供应商ID
     * @param ids
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/mergePartMateriel/{materiel_purchase_time}/{materiel_arrived_time}/{payment_date}/{supplier_id}")
    public SimpleCoreResponse mergePartMateriel(@PathVariable String materiel_purchase_time,@PathVariable String materiel_arrived_time,@PathVariable String payment_date,@RequestParam Double materiel_total_price,@RequestParam Double materiel_total_kg,@PathVariable String supplier_id,@RequestParam String isUpdate,@RequestParam(value = "ids[]") Long[] ids) throws AdscmSystemException {

        String megerId = hcPlanMakePartMaterielExampleService.mergePartMateriel(materiel_purchase_time,materiel_arrived_time,payment_date,materiel_total_price,materiel_total_kg,supplier_id, isUpdate, Arrays.asList(ids));

        hcPlanMaterielPriceService.loadMaterielPrice(megerId);


        return new SimpleCoreResponse("200","物料采购并单成功",megerId);
    }

    /**
     * 程序自动计算，选择的一批待物料的采购时间、采购数量、供应商
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/calculatePurchaseMaterielTime")
    public SimpleCoreResponse calculatePurchaseMaterielTime(@RequestParam(value = "ids[]") Long[] ids) throws AdscmSystemException {

        return new SimpleCoreResponse("200","程序自动计算，选择的一批待物料的采购时间、采购数量、供应商成功",hcPlanMakePartMaterielExampleService.calculatePurchaseMaterielTime(ids));
    }



    /**
     * 物料并单查询，根据并单ID
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getMergeMateriel/{merge_id}")
    public SimpleCoreResponse mergePartMateriel(@PathVariable String merge_id) throws AdscmSystemException {

        Wrapper<HcPlanMakePartMaterielExample> wrapper = new EntityWrapper<>();
        wrapper.eq("merge_puchase_id",merge_id);
        wrapper.orderBy("materiel_purchase_time",false);

        return new SimpleCoreResponse("200","物料并单查询成功",hcPlanMakePartMaterielExampleService.selectList(wrapper));
    }
    /**
     * 发布物料采购任务
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/submitMaterielTask/{merge_id}")
    public SimpleCoreResponse submitMaterielTask(@PathVariable String merge_id) throws AdscmSystemException {

        hcPlanMakePartMaterielExampleService.submitMaterielTask(merge_id);
        return new SimpleCoreResponse("200","发布任务成功",null);
    }

    /**
     * 根据日期，获取某年某月的采购计划
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getAllMaterielExprotPrintMsg/{date}")
    public SimpleCoreResponse getAllMaterielExprotPrintMsg(@PathVariable String date) throws AdscmSystemException {
        return new SimpleCoreResponse("200","获取的采购计划成功",
                hcPlanMakePartMaterielExampleService.getAllMaterielExprotPrintMsg(date));
    }



}
